package com._54programer.demo.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author SN
 * @date 2019/11/28
 * @description 文件拷贝
 */
public class Demo3 {

    public static void main(String[] args) throws Exception{

        //读a.txt文件通道
        File file = new File("E:\\a.txt");
        FileInputStream fis = new FileInputStream(file);
        FileChannel fisChannel = fis.getChannel();

        //写文件通道
        FileOutputStream fos = new FileOutputStream("E:\\b.txt");
        FileChannel fosChannel = fos.getChannel();

        //创建buffer, 然后从fisChannel写入到buffer, 然后再读取buffer到fosChannel
        ByteBuffer buffer = ByteBuffer.allocate((int)file.length());
        fisChannel.read(buffer);
        buffer.flip();
        fosChannel.write(buffer);

        //关闭输入流和输出流
        fis.close();
        fos.close();

    }


}
